package com.dx.weichat.weichat.wechatUtils;

import okhttp3.*;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.net.URLEncoder;

/**
 * @author: 丁鑫
 * @date: 2018/10/7/0007
 * @description: WebService util webService 接口
 */
@Component
public class WSUtil {

	private static String weatherWSUrl;

	private static String locationUrl;

	private static String bakWeatherUrl;

	private static String baidumapKey;


	private static Logger logger = LoggerFactory.getLogger(WSUtil.class);
	public static String getWeatherInfo(String theCityCode){
		String res = "";
		try {
			OkHttpClient client = new OkHttpClient();
			MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
			String VmediaType = "theCityCode="+URLEncoder.encode(theCityCode,"UTF-8")+"&theUserID=";
			RequestBody body = RequestBody.create(mediaType, VmediaType);
			Request request = new Request.Builder()
					.url(weatherWSUrl)
					.post(body)
					.addHeader("Content-Type", "application/x-www-form-urlencoded")
					.addHeader("Cache-Control", "no-cache")
					.addHeader("Postman-Token", "67d4b84c-10d6-18dc-a533-33a162bd693d")
					.build();

			Response response = client.newCall(request).execute();
			res = response.body().string();
		}catch (Exception e){
			logger.error(e.getMessage());
		}
		return res;
	}

	/**
	 * @author:dx
	 * @date:2018/10/8/0008
	 * @time:15:10
	 * @description:根据经纬度获取城市
	 * @param longitude 经度
	 * @param latitude 纬度
	 * @return
	 */
	public static String getCityByll(String longitude,String latitude) {
		String res="";
		try{
			OkHttpClient client = new OkHttpClient();

			Request request = new Request.Builder()
					.url(locationUrl+latitude+","+longitude+"&output=json&key="+baidumapKey)
					.get()
					.addHeader("Cache-Control", "no-cache")
					.addHeader("Postman-Token", "5a79d973-4352-a095-e5e7-854f011dbcbf")
					.build();
			Response response = client.newCall(request).execute();
			res = response.body().string();
		}catch (Exception e){
			logger.error(e.getMessage());
		}
		String admName = "";
		if(!StringUtils.isEmpty(res)) {
			JSONObject jsonObj = new JSONObject(res);
			admName = jsonObj.getJSONObject("result").getJSONObject("addressComponent").getString("city");

		}
		return "".equals(admName)?"无锡":admName.substring(0,2);
	}

	/**
	 * @author:dx
	 * @date:2018/10/8/0008
	 * @time:15:11
	 * @description: 获取天气情况
	 * @param cityName 城市名称
	 * @return
	 */
	public static String bakGetWeather(String cityCode){
		String res="";
		try{
			if(!StringUtils.isEmpty(cityCode))
			{
				OkHttpClient client = new OkHttpClient();
				Request request = new Request.Builder()
					.url(bakWeatherUrl+cityCode)
					.get()
					.addHeader("Cache-Control", "no-cache")
					.addHeader("Postman-Token", "fa037b11-9271-a7a6-b519-ddcbf817a704")
					.build();
				Response response = client.newCall(request).execute();
				res = response.body().string();
			}
		}catch (Exception e){
			logger.error(e.getMessage());
		}
		return res;
	}

	/**@Value 这种方式不能给静态变量注入值 所以使用这种方法折中一下*/
	@Value("${WS.weatherWSUrl}")
	public void setWeatherWSUrl(String weatherWSUrl) {
		WSUtil.weatherWSUrl = weatherWSUrl;
	}
	@Value("${WS.locationUrl}")
	public void setLocationUrl(String locationUrl) {
		WSUtil.locationUrl = locationUrl;
	}
	@Value("${WS.bakWeather}")
	public void setBakWeatherUrl(String bakWeatherUrl) {
		WSUtil.bakWeatherUrl = bakWeatherUrl;
	}

	@Value("${baidumapKey}")
	public static void setBaidumapKey(String baidumapKey) {
		WSUtil.baidumapKey = baidumapKey;
	}
}
